package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.util.BooleanUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class SwitchStyle extends BasicStyle {

    private Boolean disabled; //禁用
    private Integer falseValue = BooleanKey.FALSE.getCode();
    private Integer trueValue = BooleanKey.TRUE.getCode();
    public SwitchStyle(String label, Object value, Boolean disabled){
        super(label, value);
        this.disabled = disabled;
        this.type = FormKey.SWITCH.getCode();
    }
    public SwitchStyle(String label, Object value, Boolean disabled, String desc){
        this(label, value,disabled);
        this.desc = desc;
    }

    public SwitchStyle(){}
    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof SwitchStyle)){
            throw new ModelException("校验style类类型错误");
        }
        SwitchStyle style = (SwitchStyle) o;
        if(!(style.getDisabled() instanceof Boolean)){
            throw new ModelException("disabled传值错误");
        }
        if( !style.getDisabled() && !style.getDisabled()){
            super.verify(o);
            Integer value = BooleanUtils.toInt(style.getValue());
            if(!value.equals(BooleanKey.FALSE.getCode()) && !value.equals(BooleanKey.TRUE.getCode())){
                throw new ModelException(style.getLabel()+"传值错误");
            }
        }
        return true;
    }
}
